﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;



namespace ELookintoTXWordControl
{
    class DismissHelper
    {
        // http://www.pinvoke.net/default.aspx/user32.FindWindow

        [DllImport("user32.dll", SetLastError = true)]

        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]

        static extern System.IntPtr FindWindowByCaption(int ZeroOnly, string lpWindowName);

        // http://pinvoke.net/default.aspx/user32.SetForegroundWindow

        [DllImport("user32.dll")]

        [return: MarshalAs(UnmanagedType.Bool)]

        static extern bool SetForegroundWindow(IntPtr hWnd);


        static bool ex = false;
        public static void dismiss()
        {

            while (true)
            {
                //if (ex) break;
                string sWindowTitle = "TX Text Control Trial Version";

                Console.WriteLine("Looking for a window with the title '{0}'.", sWindowTitle);

                IntPtr hwnd = FindWindowByCaption(0, sWindowTitle);

                if (IntPtr.Zero != hwnd)
                {

                    Console.WriteLine("Found window with hwnd = '0x{0}'.", hwnd.ToString("X8"));

                    if (SetForegroundWindow(hwnd))
                    {

                        Console.WriteLine("Made that window the active window.");

                        SendKeys.SendWait("{ENTER}");
                        //ex = true;
                       
                        Console.WriteLine("Sent the ENTER key to that window.");

                    }


                }

                Thread.Sleep(1000);

                

            }

        }
    }


   
}
